package com.smt.take.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.daimajia.slider.library.SliderLayout
import com.daimajia.slider.library.SliderTypes.TextSliderView
import com.smt.take.R
import com.smt.take.model.beans.Seller
import com.smt.take.ui.activity.BusinessActivity
import com.smt.take.utils.TakeOutApp
import com.squareup.picasso.Picasso
import org.jetbrains.anko.find

class HomeRvAdapter(private val context: Context?) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        const val TYPE_TITLE = 0
        const val TYPE_SELLER = 1
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) {
            TYPE_TITLE
        } else {
            TYPE_SELLER
        }
    }

    private var mData: MutableList<Seller> = mutableListOf()
    private var urlMaps: HashMap<String, String> = HashMap()

    fun setData(data: MutableList<Seller>) {
        mData = data
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_TITLE -> TitleHolder(View.inflate(context, R.layout.item_title, null))
            TYPE_SELLER -> SellerHolder(
                View.inflate(
                    context,
                    R.layout.item_seller,
                    null
                )
            )
            else -> TitleHolder(View.inflate(context, R.layout.item_title, null))
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            TYPE_TITLE -> (holder as TitleHolder).bindData("我是大哥------------------------")
            TYPE_SELLER -> (holder as SellerHolder).bindData(mData[position - 1])
        }

    }

    override fun getItemCount(): Int {
        return if (mData.size > 0) {
            mData.size + 1
        } else {
            0
        }
    }

    inner class TitleHolder(item: View) : RecyclerView.ViewHolder(item) {
        private val slideLayout: SliderLayout = item.findViewById(R.id.slider)

        fun bindData(data: String) {
            if (urlMaps.size == 0) {
                urlMaps["新鲜水果"] =
                    "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3220597185,1552383195&fm=26&gp=0.jpg"
                urlMaps["新鲜肉类"] =
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180824%2Fbc7915636e8a465192360d1194c6e5a0.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617849992&t=4b4ce10f8281ced4c3f0aa17c03b7a25"
                urlMaps["新鲜蔬菜"] =
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.fzfybj.com%2Fhkhoutai%2Fkindeditor-4.1.10%2Fattached%2Fimage%2F20200131%2F20200131083325_12307.jpeg&refer=http%3A%2F%2Fwww.fzfybj.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617850076&t=2ab67051939c789e37546b6087689fd0"
                urlMaps["新鲜牛奶"] =
                    "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1819818805,3859275563&fm=26&gp=0.jpg"

                for ((key, value) in urlMaps) {
                    val textSliderView = TextSliderView(context)
                    textSliderView.description(key).image(value)
                    slideLayout.addSlider(textSliderView)
                }
            }
        }
    }

    inner class SellerHolder(item: View) : RecyclerView.ViewHolder(item) {
        lateinit var data: Seller
        private val tvTitle: TextView = item.find(R.id.tv_title)
        private val ivLog: ImageView = item.find(R.id.seller_logo)
        private val ratingBar: RatingBar = item.find(R.id.ratingBar)
        private val tvSale: TextView = item.find(R.id.tv_home_sale)
        private val tvPrice: TextView = item.find(R.id.tv_home_send_price)
        private val tvDistance: TextView = item.find(R.id.tv_home_distance)

        init {
            item.setOnClickListener {
                val intent = Intent(context, BusinessActivity::class.java)
                // 去取客户在商家是否有缓存信息
                // 逐层读取，判断这家店是否有缓存
                var hasSelectInfo = false
                val count = TakeOutApp.instance.queryCacheSelectedInfoBySellerId(data.id.toInt())
                if (count > 0) {
                    hasSelectInfo = true
                }
                intent.putExtra("seller",data)
                intent.putExtra("hasSelectInfo", hasSelectInfo)
                context?.startActivity(intent)
            }
        }

        @SuppressLint("SetTextI18n")
        fun bindData(data: Seller) {
            this.data = data
            tvTitle.text = data.name
            Picasso.with(context).load(data.icon).into(ivLog)
            ratingBar.rating = data.score.toFloat()
            tvSale.text = "月售${data.sale}单"
            tvPrice.text = "￥${data.sendPrice}起送/配送费￥${data.deliveryFee}"
            tvDistance.text = data.distance
        }
    }
}